package com.hilaver.dzmis.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.MathContext;
import java.sql.Blob;
import java.util.Date;

public class StringUtils {
	public static String firstLetterToUpcase(String in) {
		if(in == null || in.length() == 0){
			return in;
		}
		return in.substring(0, 1).toUpperCase() + in.substring(1);
	}
	
	public static String getFileExtension(String fileName){
		if(fileName == null || fileName.length() == 0){
			return fileName;
		}
		String extension = fileName.substring(fileName.lastIndexOf("."));
		return extension == null?".jpg":extension;
	}
	
	public static Boolean isEmpty(String in){
		if(in == null || in.length() == 0){
			return true;
		}else{
			return false;
		}
	}
	
	public static Date phaseDate(String strDate){
		// the format of strDatea is like '12/10/2008(d/m/Y)'
		if(strDate == null) return null;
		String[] date = strDate.split("/");
		Date rtn = new Date();
		rtn.setMonth(Integer.parseInt(date[1]) - 1);
		rtn.setDate(Integer.parseInt(date[0]));
		rtn.setYear(Integer.parseInt(date[2]) - 1900);
		return rtn;
	}
	
	public static String phaseStrDate(String strDate){
		// the format of strDatea is like '12/10/2008(d/m/Y)'
		String[] dates = strDate.split("/");
		Integer month = Integer.parseInt(dates[1]);
		Integer date = Integer.parseInt(dates[0]);
		Integer year = Integer.parseInt(dates[2]);
		return year.toString() + "-" + (month<10?("0" + month.toString()):month.toString()) + "-" + (date<10?("0" + date.toString()):date.toString());
	}
	
	public static String toStringLine(Date date){
		if(date == null) return "";
		Integer month = date.getMonth() + 1;
		return (date.getDate()<10?("0"+date.getDate()):date.getDate()) + "-" + (month<10?("0" + month.toString()):month.toString()) + "-" + (date.getYear() + 1900);
	}
	
	public static String toString(Date date){
		if(date == null) return "";
		Integer month = date.getMonth() + 1;
		return (date.getDate()<10?("0"+date.getDate()):date.getDate()) + "/" + (month<10?("0" + month.toString()):month.toString()) + "/" + (date.getYear() + 1900);
	}
	
	public static String toFullString(Date date){
		if(date == null) return "";
		Integer month = date.getMonth() + 1;
		String rtn = (date.getDate()<10?("0"+date.getDate()):date.getDate()) + "/" + (month<10?("0" + month.toString()):month.toString()) + "/" + (date.getYear() + 1900); 
		return (date.getHours()<10?("0"+date.getHours()):date.getHours()) + ":" + (date.getMinutes()<10?("0"+date.getMinutes()):date.getMinutes()) + ":" + (date.getSeconds()<10?("0"+date.getSeconds()):date.getSeconds()) + " " + rtn;
	}
	
	public static String to4Digital(Integer in){
		String inStr = in.toString();
		int lenght = inStr.length();
		if(inStr.length() < 4){
			for(int i = 4; i > lenght; i --){
				inStr = "0" + inStr;
			}
		}
		return inStr;
	}
	
	public static String to6Digital(Integer in){
		String inStr = in.toString();
		int lenght = inStr.length();
		if(inStr.length() < 6){
			for(int i = 6; i > lenght; i --){
				inStr = "0" + inStr;
			}
		}
		return inStr;
	}
	
	public static String toNDigital(Double in, int digital){
		BigDecimal d = new BigDecimal(in);
		MathContext mc = new MathContext(digital);
		d = d.round(mc);
		return d.toString();
	}
	
	public static String getDateHalfYearBefore(){
		Date halfYearBefore = new Date((System.currentTimeMillis()/1000 - 60 * 60 * 24 * 30 * 6)*1000);
		return phaseStrDate(toString(halfYearBefore));		
	}
	
	public static String toString(Double d, Integer scale){
		if(d == 0 || d == null || scale == null){
			return "";
		}
		return (new BigDecimal(d).setScale(scale, BigDecimal.ROUND_HALF_UP)).toString();
	}
	
	public static String toString(Float d, Integer scale){
		if(d == 0 || d == null || scale == null){
			return "";
		}
		return (new BigDecimal(d).setScale(scale, BigDecimal.ROUND_HALF_UP)).toString();
	}
	
	public static void writeFile(String filePath, Blob fileBlob){
		if(fileBlob == null){
			return;
		}
		File file = new File(filePath);
		if(file.exists()){
			return;
		}
		InputStream is = null;
		FileOutputStream fos = null;
		try {
			is = fileBlob.getBinaryStream();
			fos = new FileOutputStream(filePath);
			byte[] buf = new byte[102400];
			   
			int len;
			while((len = is.read(buf))!= -1){
			    fos.write(buf,0,len);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				if(fos != null){
					fos.close();
				}
				if(is != null){
					is.close();
				}
			} catch (IOException e) {
			}
		}
	}
	
	public static void main(String[] args){
		System.out.println(toNDigital(0.9894d, 4));
	}
}
